import dayjs from 'dayjs'

const TOKEN_KEY = 'AUTH_INFO'
const USER_KEY = 'USER_INFO'

//控制 webViewUrl
let webViewUrl
if (process.env.NODE_ENV === 'development') {
    webViewUrl = ''
} else {
    webViewUrl = ''
}


const util = {
    webViewUrl: webViewUrl,
    setToken(token) {
        if (!token) {
            uni.removeStorageSync('TOKEN_KEY')
            uni.removeStorageSync('USER_KEY')
        }
        uni.setStorageSync('TOKEN_KEY', token)
    },
    getToken() {

        var token = uni.getStorageSync('TOKEN_KEY');
        return uni.getStorageSync('TOKEN_KEY');

    },
    getUserInfo() {
        const userInfoJson = uni.getStorageSync('USER_KEY');
        if (!userInfoJson) return undefined
        // return JSON.parse(userInfoJson)
        return userInfoJson

    },

    getMerchantId() {
        const merchantId = uni.getStorageSync('merchantId');
        if (!merchantId) return undefined
        return merchantId
    },

    /**
     * 返回appVersion
     * @returns {'v0'|'v1'}
     */
    appVersionGet() {
        return uni.getStorageSync('appVersion') || 'v1'
    },
    goHome() {
        uni.reLaunch({
            url: '/pages/index/index'
        })
    },
    goLogin() {
        uni.navigateTo({
            url: '/pages/login/login'
        })
    },
    validIdCard(idCard) {
        const regex =
            /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/
        return regex.test(idCard)
    },

    //身份证和手机号码脱敏
    maskNumber(number) {
        if (number.length === 11) {
            return `${number.substr(0, 3)}****${number.substr(7)}`;
        } else if (number.length === 18) {
            return `${number.substr(0, 6)}********${number.substr(14)}`;
        }
        return number;
    },

    isLogin(isGoLogin) {
        if (util.getToken()) {
            return true
        }
        if (isGoLogin) util.goLogin()
        return false;
    },
    dateStr(dateStr) {
        // console.log('date = ', dayjs(dateStr).format('YYYY-MM-DD'))
        return dayjs(dateStr).format('YYYY-MM-DD');
    },
    back(time = 20) {
        setTimeout(() => uni.navigateBack(), time)
    },
    goPage(page, time = 500) {
        setTimeout(() => uni.navigateTo({
            url: page
        }), time)
    },
    /**
     * 对比开始时间是否小于结束时间
     * @param {Object} startTime 开始时间
     * @param {Object} endTime 结束时间
     */
    timeDifference(startTime, endTime) {
        if (!startTime || 0 === startTime.length || !endTime || 0 === endTime.length) {
            return false;
        }
        var sDate = new Date(startTime.replace(/-/g, '/'));
        var eDate = new Date(endTime.replace(/-/g, '/'));
        var res = eDate - sDate;
        if (res < 0) {
            return false;
        } else {
            return true;
        }
    },



}

export default util